$(function(){
    /*************************************/

    // 页面信息
    getItemType();
    // 获取项目类型 --ok
    function getItemType() {
        let P_raceHead = new Promise( (resolve, reject)=>{
            $.ajax({
                url : '/manage/racetype/get_raceType.do',
                type: 'post',
                data : {
                    parentId : 0
                },
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data) {
                    if( data.status == 0 ){
                        resolve( data.data );
                    }else if( data.status == 10 ){
                        reject( data );
                    }
                }
            });
        } );
        P_raceHead.then( ( aData )=>{
            for(let i=0; i<aData.length; i++){
                let DOM = $('<li data-id="'+ aData[i].racetypeno +'">' +
                    '           <a  href="javascript:;">'+ aData[i].racetype +'</a>' +
                    '        </li>');

                $('#dropdown-menu').append( DOM );
            }
            $('#dropdown-menu').find('li').eq(0).click();
        }).catch( (data)=>{
            if( data.status == 10 ){
                spop_waring( data.msg, function () {}, outLogin )
            }
        } )
    }

    // 项目类型 --ok
    $('#dropdown-menu').on('click','li',function () {
        $('#table-body').html('');

        let thisId = $(this).attr( 'data-id' );

        let sonRecaType = new Promise( (resolve, reject)=>{
            $.ajax({
                url : '/manage/racetype/get_raceType.do',
                type: 'post',
                data : {
                    parentId : thisId
                },
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data) {
                    if( data.status == 0 ){
                        resolve( data.data );
                    }else if( data.status == 10 ){
                        reject( data );
                    }
                }
            });
        } );
        sonRecaType.then( (aData)=>{
            To_reacTyoe( aData );
        } ).catch( (data)=>{
            if( data.status == 10 ){
                spop_waring( data.msg, function () {}, outLogin )
            }
        } );
    });

    // 全选 --ok
    $('#allChecked').on('click',function () {
        let OFF = $(this).prop('checked');
        let aChecked = $('#table-tbody').find('.sonChakbox');
        aChecked.each( (idx,item)=>{
            $(item).prop( 'checked', OFF );
        });
    });

    // 单选  -ok
    $('#table-tbody').on('change','.sonChakbox',function () {
        let aChecked = $('#table-tbody').find('.sonChakbox');
        let arr = [];

        aChecked.each(function (idx, item) {
            arr.push( item.checked );
        });

        let OFF = arr.every(function (item, key, array) {
            return item == true
        });

        $('#allChecked').prop( 'checked' , OFF );
    });

    // 单个 项目开关  -- ok
    $('#table-tbody').on('click', 'input.hidden', function () {
        let thisID = [];
        let that = $(this).parents('label');

        thisID.push( $(this).parents('tr').attr('data-id') );
        let OFF = Number( that.attr('data-status') );
        if( OFF ){
            // 1 ==> 0  开启
            $.ajax({
                url : '/manage/racetype/startDeclaring.do',
                type : 'post',
                data : {
                    racetypenoList : thisID
                },
                xhrFields : {
                    withCredentials: true
                },
                crossDomain : true,
                success: function (data) {
                    if(data.status == 0){
                        spop_ok( data.msg, function () {
                            that.addClass('active').attr('data-status', 0);
                        });
                    }else if( data.status == 10 ){
                        spop_waring( data.msg, function () {}, outLogin )
                    }else{
                        spop_er( data.data );
                    }
                }
            });
        }
        else{
            // 0 ==> 1  关闭
            $.ajax({
                url : '/manage/racetype/stopDeclaring.do',
                type : 'post',
                data : {
                    racetypenoList : thisID
                },
                xhrFields : {
                    withCredentials: true
                },
                crossDomain : true,
                success: function (data) {
                    if(data.status == 0){
                        spop_ok( data.msg, function () {
                            that.removeClass('active').attr('data-status', 1 );
                        });
                    }else if( data.status == 10 ){
                        spop_waring( data.msg, function () {}, outLogin )
                    }else{
                        spop_er( data.data );
                    }
                }

            });
        }
    });

    // 开启申报 --ok
    $('#startDeclare').on('click', function () {
        let activeOBJ = getActiveBox();
        if( activeOBJ ){
            openDeclare( activeOBJ );
        }else{
            spop_waring('未选中项目');
        }
    });
    // 关闭申报 --ok
    $('#stopDeclare').on('click', function () {
        let activeOBJ = getActiveBox();
        if( activeOBJ ){
            closeDeclare( activeOBJ );
        }else{
            spop_waring('未选中项目');
        }
    });

    // 设定开始时间
    $('#startDecTime').on('click', function () {
        let startTime = $(this).parent().prev().val();
        let activeTr = getActiveBox();

        if( startTime && activeTr ){
            openDecTime( startTime, activeTr );
        }else if( !startTime ){
            spop_waring('未选定开始时间');
        }else if( !activeTr ){
            spop_waring('未选中项目');
        }
    });
    // 设定结束时间
    $('#stopDecTime').on('click', function () {
        let startTime = $(this).parent().prev().val();
        let activeTr = getActiveBox();

        if( startTime && activeTr ){
            closeDecTime( startTime, activeTr );
        }else if( !startTime ){
            spop_waring('未选定结束时间');
        }else if( !activeTr ){
            spop_waring('未选中项目');
        }
    });

    //****************************************
    // 开启申报 --ok
    function openDeclare( obj ) {
        let aID = obj.aID,
            aInput = obj.aInput;
        $.ajax({
            url : '/manage/racetype/startDeclaring.do',
            type : 'post',
            data : {
                racetypenoList : aID
            },
            xhrFields : {
                withCredentials: true
            },
            crossDomain : true,
            success: function (data) {
                if(data.status == 0){
                    spop_ok( data.msg, function () {
                        styleChange( aInput, 0 );
                     });
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }else{
                    spop_er( data.data );

                }
            }

        })
    }
    // 关闭申报--ok
    function closeDeclare( obj ) {
        let aID = obj.aID,
            aInput = obj.aInput;
        $.ajax({
            url : '/manage/racetype/stopDeclaring.do',
            type : 'post',
            data : {
                racetypenoList : aID
            },
            xhrFields : {
                withCredentials: true
            },
            crossDomain : true,
            success: function (data) {
                console.log(data);
                if(data.status == 0){
                    spop_ok( data.msg, function () {
                        styleChange( aInput, 1 );
                    });
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }else{
                    spop_er( data.data );
                }
            }

        })
    }

    // 开始时间
    function openDecTime( sDate, oTr ) {
        let aId = oTr.aID;
        $.ajax({
            url : '/manage/racetype/setStartTime.do',
            type : 'post',
            data : {
                startTime : sDate,
                racetypenoList : aId
            },
            xhrFields : {
                withCredentials: true
            },
            crossDomain : true,
            success: function (data) {
                if(data.status == 0){
                    spop_ok( data.msg, function () {
                        changeDate( oTr.aInput, sDate , 0 )
                    } );
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }else{
                    spop_er( data.msg );
                }
            }

        })
    }
    // 结束时间
    function closeDecTime( sDate, oTr ) {
        let aId = oTr.aID;
        $.ajax({
            url : '/manage/racetype/setStopTime.do',
            type : 'post',
            data : {
                stopTime : sDate,
                racetypenoList : aId
            },
            xhrFields : {
                withCredentials: true
            },
            crossDomain : true,
            success: function (data) {
                if(data.status == 0){
                    spop_ok( data.msg, function () {
                        changeDate( oTr.aInput, sDate , 1 )
                    });
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }else{
                    spop_er( data.msg );
                }
            }

        })
    }

    // 获取选中的按钮 --ok
    function getActiveBox() {
        let oActiveBox = {
            aID : [],
            aInput : []
        };
        let aChecked = $('#table-tbody').find('.sonChakbox');

        aChecked.each(function ( idx ) {
            if($(this).is(":checked")){
                oActiveBox.aID.push( $(this).parents('tr').attr('data-id') );
                oActiveBox.aInput.push( $(this) );
            }
        });

        if( oActiveBox.aID.length ){
            return oActiveBox;
        }else{
            return false;
        }

    }

    // 申报类型渲染 --ok
    function To_reacTyoe( aData ){
        $('#table-tbody').html('');

        let startTime = '',
            stopTime = '';

        aData.forEach( (item,idx)=>{
            startTime = item.starttime || '---- :-- : --';
            stopTime = item.stoptime || '---- : -- : --';

            let DOM = $('<tr data-id="'+ item.racetypeno +'">' +
                ' <td style="padding-left: 15px;padding-bottom:0" width="45">' +
                '     <input type="checkbox" class="sonChakbox">' +
                ' </td>' +
                ' <td><a href="./itemType-info.html?racetypeno='+ item.racetypeno +'">'+ item.racetype +'</a></td>' +
                ' <td class="startTime">'+ startTime +'</td>' +
                ' <td class="stopTime">'+ stopTime +'</td>' +
                ' <td>' +
                '     <div class="System-itemChange">' +
                '         <label class="Text-cup" data-status="'+ item.status +'">' +
                '             <input type="checkbox" class="hidden">' +
                '             <span class="box"></span>' +
                '         </label>' +
                '     </div>' +
                ' </td>' +
                '</tr>');

            if( item.status == 0 ){
                DOM.find('label').addClass('active');
            }

            $('#table-tbody').append( DOM );
        });
    }

    // styleChange --ok
    function styleChange( aDom , status ) {
        aDom.forEach(function (item, idx, array) {
            if( status ){
                // 关闭
                item.parents('tr').find('label').removeClass('active').attr('data-status', status);
            }else{
                // 开启
                item.parents('tr').find('label').addClass('active').attr('data-status', status);
            }
            console.log(item);
            item.prop('checked',false);
        });
        $('#allChecked').prop('checked',false);
    }

    // dateChange
    function changeDate( aInput, date , type) {

        aInput.forEach(function (item, index) {
            if( type ){
                // stop
                item.parents('tr').find('.stopTime').text( date );
            }else{
                // start
                item.parents('tr').find('.startTime').text( date );
            }
        })
    }
    //****************************************

});